<?PHP

class author{

	var $id			= null;
	var $isAdmin	= false;
	var $nameFirst 	= '';
	var $nameLast 	= '';
	var $username 	= '';
	var $email		= '';
	var $regDate	= '';

	/********************************************
	* Creation and loading Methods
	*
	* __construct($id)
	*	- Constructor
	*
	* _load($id)
	*	- loads the data
	*	- true/false on success/failure
	*
	********************************************/

	function __construct($id = null){
		$this->_load($id);
	}

	function _load($id){
		$sql = "SELECT * FROM authors WHERE AuthorID = ?";
		$parms = array($id);
		$query = $GLOBALS['db']->prepare_execute($sql, $parms);
		if($row = $query->fetch()){
			$this->id			= $row['AuthorID'];
			$this->nameFirst 	= $row['FirstName'];
			$this->nameLast 	= $row['LastName'];
			$this->username 	= $row['UserName'];
			$this->email		= $row['Email'];
			$this->regDate		= $row['RegistrationDate'];

			if($this->username == 'Admin' || $this->username == 'admin')
				$this->isAdmin	= true;

			return true;
		}

		return false;
	}

	/********************************************
	* Data Methods
	*
	* articles($count = false)
	* 	- returns author's articles
	*
	********************************************/

	function articles($count = false){
		$sql = "SELECT FileID, Title, Articledata, Approved FROM articles WHERE AuthorID=? ORDER by FileID";
		if($count)
			$sql .= " LIMIT ".intval($count);
		$parms = array($this->id);
		$query = $GLOBALS['db']->prepare_execute($sql, $parms);

		return $query->fetchAll();
	}

	/********************************************
	* Editing Methods
	*
	* change_password($password)
	* 	- Changes a user's password
	*
	********************************************/

	function change_password($password){
		$sql = "UPDATE authors SET Passwd = PASSWORD(?) WHERE AuthorID = ?";
		$parms = array($password, $this->id);

		return $GLOBALS['db']->prepare_execute($sql, $parms) ? true : false;
	}

	/********************************************
	* Factory Methods
	*
	* login($login, $password)
	* 	- Creates a class on acceptance of login and password
	*	- returns false on failure
	*
	* Create($username, $nameFirst, $nameLast, $email, $passwd)
	*	- Creates a new user and returns class on pass
	*	- Returns false if username is taken
	*
	* Load($id)
	*	- Loads an author based on ID returned false on failure
	*
	********************************************/

	public static function login($l, $p){
		$sql = "SELECT AuthorID FROM authors WHERE UserName = ? AND Passwd = PASSWORD(?) ";
		$parms = array($l,$p);
		$query = $GLOBALS['db']->prepare_execute($sql, $parms);
		if($data = $query->fetch()){
			return new author($data['AuthorID']);
		}

		return false;
	}

	public static function create($username, $nameFirst, $nameLast, $email, $passwd){
		$sql = "SELECT UserName FROM authors WHERE UserName = ?";
		$parms = array($username);
		$query = $GLOBALS['db']->prepare_execute($sql, $parms);

		if($data = $query->fetch())
			return false;

		$sql = "INSERT INTO authors (UserName, FirstName, LastName, Email, Passwd, RegistrationDate) VALUES (?,?,?,?,PASSWORD(?),NOW())";
		$parms = array($username, $nameFirst, $nameLast, $email, $passwd);
		$query = $GLOBALS['db']->prepare_execute($sql, $parms);

		return new author($GLOBALS['db']->lastInsertId());
	}

	public static function load($id){
		$a = new author($id);
		if($a->id)
			return $a;

		return false;
	}

}

?>